//
//  ZHCommonCell.m
//  新浪微博ZH
//
//  Created by wangzhaohui-Mac on 14-7-22.
//  Copyright (c) 2014年 com.Itheima. All rights reserved.
//

#import "WKCommonCell.h"
#import "WKCommonItem.h"
//#import "ZHCommonLabelItem.h"
#import "WKCommonSwitchItem.h"
#import "WKBadgeView.h"
#import "WKCommonArrowItem.h"

@interface WKCommonCell ()
/**箭头*/
@property (nonatomic,strong) UIImageView *rightArrow;
/**开关*/
@property (nonatomic,strong) UISwitch *rightSwitch;
/**标签*/
@property (nonatomic,strong) UILabel *rightLabel;
/**提醒文字*/
@property (nonatomic,strong) WKBadgeView *badgeView;
@end

@implementation WKCommonCell


#pragma mark - 懒加载cell右边的view
/**箭头*/
- (UIImageView *)rightArrow
{
    if (_rightArrow == nil) {
        _rightArrow = [[UIImageView alloc] initWithImage:[UIImage imageWithName:@"common_icon_arrow"]];
    }
    return _rightArrow;
}
/**开关*/
- (UISwitch *)rightSwitch
{

    if (_rightSwitch == nil) {
        _rightSwitch = [[UISwitch alloc] init];
    }

    return _rightSwitch;
}
/**标签*/
- (UILabel *)rightLabel
{
    if (_rightLabel == nil) {
        _rightLabel = [[UILabel alloc] init];
        _rightLabel.font = [UIFont systemFontOfSize:13];
        _rightLabel.textColor = [UIColor lightGrayColor];
    }
    return _rightLabel;
}
/**提醒文字*/
- (WKBadgeView *)badgeView
{
    if (_badgeView == nil) {
        _badgeView = [[WKBadgeView alloc] init];
    }
    return _badgeView;
}



#pragma mark - 初始化方法
+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"cell";
    WKCommonCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[WKCommonCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
    }
    return cell;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //设置标题的字体
        self.detailTextLabel.font = [UIFont systemFontOfSize:11];
        self.textLabel.font = [UIFont boldSystemFontOfSize:15];
    
        //设置cell的背景颜色
//        self.backgroundColor = [UIColor clearColor];
        //取消显示选中时的阴影
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        //设置背景view
        self.backgroundView = [[UIImageView alloc] init];
        self.selectedBackgroundView = [[UIImageView alloc] init];
    }
    return self;
}

- (void)setItem:(WKCommonItem *)item
{
    _item = item;
    
    //1,设置基本数据
    self.imageView.image = [UIImage imageWithName:item.icon];
    self.textLabel.text = item.title;
    self.detailTextLabel.text = item.subTitle;
   

    //2,设置cell右边的内容
    if (item.badgeValue) {// 紧急情况：右边有提醒数字
        self.badgeView.badgeValue = item.badgeValue;
        self.accessoryView = self.badgeView;
        
    }else if ([item isKindOfClass:[WKCommonSwitchItem class]])
    {
        self.accessoryView = self.rightSwitch;
    }else if ([item isKindOfClass:[WKCommonArrowItem class]])
    {
        self.accessoryView = self.rightArrow;
    }
//    else if ([item isKindOfClass:[ZHCommonLabelItem class]])
//    {
//        ZHCommonLabelItem *labelItem = (ZHCommonLabelItem *)item;
//        //设置文字
//        self.rightLabel.text = labelItem.text;
//        //根据文字的计算尺寸
//        self.rightLabel.size = [labelItem.text sizeWithFont:self.rightLabel.font];
//        self.accessoryView = self.rightLabel;
//    }
    else
    {//取消右边的内容
        self.accessoryView = nil;
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
//    self.x = ZHStatusCellInset;
//    self.width = 300;
    //调整子标题的x值
    self.detailTextLabel.x = CGRectGetMaxX(self.textLabel.frame) + 10;

}

#pragma mark - 公共方法
- (void)setIndexPath:(NSIndexPath *)indexPath rowInSection:(int)rows
{
    //1,取出背景view
    UIImageView *bgView = (UIImageView *)self.backgroundView;
    UIImageView *selectedGgView = (UIImageView *)self.selectedBackgroundView;
    
    //2,设置背景图片
    if (rows == 1) {
        bgView.image = [UIImage resizeImageWithName:@"common_card_background"];
        selectedGgView.image = [UIImage resizeImageWithName:@"common_card_background_highlighted"];
    }else if (indexPath.row == 0)//代表首行
    {
        bgView.image = [UIImage resizeImageWithName:@"common_card_top_background"];
        selectedGgView.image = [UIImage resizeImageWithName:@"common_card_top_background_highlighted"];
    }else if (indexPath.row == rows - 1)//代表末行
    {
        bgView.image = [UIImage resizeImageWithName:@"common_card_bottom_background"];
        selectedGgView.image = [UIImage resizeImageWithName:@"common_card_bottom_background_highlighted"];
    }else
    {//代表中间
        bgView.image = [UIImage resizeImageWithName:@"common_card_middle_background"];
        selectedGgView.image = [UIImage resizeImageWithName:@"common_card_middle_background_highlighted"];
    }

}

@end
